<template>
  <ex-dialog width="50%" class="ex-diolog" :title="title" :visible="visible" :close-on-click-modal="false"
    @closeDialog="handleClose">
    <div class="main" v-loading="loading">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-radio-group v-model="ruleForm.feeType" @change="handleRadio">
            <el-radio :label="1">
              <el-form-item label="神椰CoCo：" :prop="ruleForm.feeType == 1 ? 'num' : ''" key='1'>
                <el-input v-model="ruleForm.num" placeholder="请输入收取数量"></el-input>
              </el-form-item>
            </el-radio>
            <el-radio :label="3">
              <el-form-item label="椰分" :prop="ruleForm.feeType == 3 ? 'numNew' : ''" key='3'>
                <el-input v-model="ruleForm.numNew" placeholder="请输入收取数量"></el-input>
              </el-form-item>
            </el-radio>
            <el-radio :label="2">
              <el-form-item label="现金" :prop="ruleForm.feeType == 2 ? 'money' : ''" key='2'>
                <el-input v-model="ruleForm.money" placeholder="请输入收取金额"></el-input>
              </el-form-item>
            </el-radio>
        </el-radio-group>
      </el-form>
    </div>
    <div class="button_box">
      <el-button type="primary"  @click="submitForm('ruleForm')">保存提交</el-button>
      <el-button @click="handleClose">取消</el-button>
    </div>
  </ex-dialog>
</template>
<script>
  // components
  import Loading from "@/components/Loading/index";
  // api
 import {
    setFee,
    configDetail
  } from "@/api/examplesOf";

  export default {
    components: {
      Loading,
    },
    props: {
      visible: Boolean,
      id: String,
    },
    data() {
      return {
        title:'手续费配置',
        loading: false,
        value1: '',
        radio: '',
        ruleForm: {
          num: '',
          money: '',
          numNew: '',
          feeType: 1,//手续费类型 1神椰coin 2现金
        },
        rules: {
          num: [{
            required: true,
            message: "请输入收取数量",
            trigger: "blur"
          }],
          numNew: [{
            required: true,
            message: "请输入收取数量",
            trigger: "blur"
          }],
          money: [{
            required: true,
            message: "请输入收取金额",
            trigger: "blur"
          }],
          feeType: [{
            required: true,
            message: "请选择",
            trigger: "change"
          }],
        },
      };
    },
    computed: {},
    created() {
        this.getDetail()
    },
    methods: {
      //单选切换
      handleRadio(e){
        
        if(e == 1){
          this.ruleForm.money = ''
          this.ruleForm.numNew = ''
        }else if(e == 2){
          this.ruleForm.num = ''
          this.ruleForm.numNew = ''
        }else if(e == 3){
          this.ruleForm.money = ''
          this.ruleForm.num = ''
        }
      },
      //获取开奖详情
      getDetail(){
        configDetail(this.id).then(response => {
          console.log('response',response)
           this.ruleForm.id =  response.result.id
          if(response.result.feeType){
            this.ruleForm.feeType =  parseInt(response.result.feeType)
           
            if(response.result.feeType == 1){
              this.ruleForm.num =  response.result.fee
            }else if(response.result.feeType == 2){ 
              this.ruleForm.money = response.result.fee
            }else if(response.result.feeType == 3){ 
              this.ruleForm.numNew = response.result.fee
            }
          }
          console.log(' this.ruleForm', this.ruleForm)
        });
      },
      //取消
      handleClose(formName) {
        this.$emit("update:visible", false);
      },
      handleCancel(){
        this.handleClose()
      },
      submitForm(formName) {
        //创建确定
        this.$refs[formName].validate((valid) => {
          if (valid) {
            var fee = ''
            if(this.ruleForm.feeType == 1){
              fee = this.ruleForm.num
            }else if(this.ruleForm.feeType == 2){
              fee = this.ruleForm.money
            }else if(this.ruleForm.feeType == 3){
              fee = this.ruleForm.numNew
            }
            var params = {
                id: this.id,
                fee: fee,
                feeType: this.ruleForm.feeType
              };
              setFee(params).then((res) => {
                this.$message({
                  message: "保存成功",
                  type: "success",
                });
                setTimeout(() => {
                  this.$emit('success')
                  this.handleClose()
                }, 1000);
              })
              .finally(() => {

              });
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
     
    },
  };

</script>
<style lang="scss" scoped>
  .ex-diolog {
    ::v-deep .ex--dialog--footer {
      border: none !important;
      padding: 0 !important;
    }

    ::v-deep .ex--dialog--main {
      padding: 10px 20px !important;
    }
  }

  .main {
    padding: 20px 20px;
  }

  .el-radio{
    display: flex;
    ::v-deep .el-radio__inner{
      margin-top: 9px;
    }
  }
  

  .carousel_box {
    width: 400px;
    margin-bottom: 20px;
  }
  .list{
    display: flex;
    justify-content: flex-start;
  }
  .button_box {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px 0;
  }

  


</style>
